package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.add;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

import java.util.ArrayList;
import java.util.List;

public class AddUserInstanceListProcessFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        singleInsert(flowContext);
        multipleInsert(flowContext);
    }

    @Override
    public String name() {
        return "添加用户";
    }

    private void singleInsert(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        User user = new User();
        user.setUsername("user0");
        user.setPassword("123456");
        user.setEmail("user0@fake.com");
        user.setMobile("12345678911");
        JSONObject extra = new JSONObject();
        extra.put("age", 0);
        user.setExtraData(extra);
        int effect = dao.insert(user);
        Assert.assertEquals(1, effect);

        flowContext.putTemporaryData("insertUser", user);
    }

    private void multipleInsert(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        List<User> userList = new ArrayList<>();
        for(int i=1;i<=10;i++){
            User user = new User();
            user.setUsername("user"+i);
            user.setPassword("123456");
            user.setEmail("user"+i+"@fake.com");
            user.setMobile("12345678911");
            JSONObject extra = new JSONObject();
            extra.put("age", i);
            user.setExtraData(extra);
            userList.add(user);
        }
        int effect = dao.insert(userList);
        Assert.assertEquals(10, effect);

        flowContext.putTemporaryData("insertUserList", userList);
    }
}
